//
//  WBNavController.swift
//  weiboD
//
//  Created by gao2015 on 2016/12/1.
//  Copyright © 2016年 gao2015. All rights reserved.
//

import UIKit

class WBNavController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置手势交互的代理
        self.interactivePopGestureRecognizer?.delegate = self;
        
        
    
        
    }
    //重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
//        print(viewControllers.count)
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
        //push后入伙vC为1的话
        if viewControllers.count > 1 {
            //如果等于2 到时候，标题为自定义的
            if viewControllers.count == 2 {
                let title = viewControllers.first!.title!
            
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
                
            }else{
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))

            }
            viewController.title = "当前的控制器是\(viewControllers.count)级控制器"
        }
    
        
    }
    @objc private func popAction(){
        
        self.popViewController(animated: true)
        
    }
    
}

extension WBNavController: UIGestureRecognizerDelegate {
    //  是否处理这次点击的边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            //  如果是跟视图控制器不让去处理这个边缘手势
            return false
        }
        
        return true
    }
}

